﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace LightningForms
{
    public class Document : BaseTextControl
    {
        private string script()
        {
            string type = "{}";
            switch (this.DocumentType)
            {
                case DocumentType.CPF: type = "_lf.Type.CPF"; break;
                case DocumentType.CNPJ: type = "_lf.Type.CNPJ"; break;
            }
            return "_lf.AddControl({ Control: '#_R_', Type: _T_ });"
                .Replace("_R_", this.ClientID)
                .Replace("_T_", type);
        }

        public DocumentType DocumentType
        {
            set { this.ViewState["DocumentType"] = value; }
            get
            {
                object value = this.ViewState["DocumentType"];
                if (value != null) return (DocumentType)value;
                return DocumentType.NONE;
            }
        }

        public Document()
        {
            this.DataType = ValidationDataType.String;
        }

        protected override void OnPreRender(EventArgs e)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, script(), true);
            base.OnPreRender(e);
        }
    }

    public enum DocumentType
    {
        NONE,
        CPF,
        CNPJ
    }
}